#include "hw.h"
#include "print.h"
#include "main.h"
#include "version.h"
#include "options.h"
#include "osutils.h"
#include "config.h"

#include <unistd.h>
#include <stdio.h>
#include <iostream>

#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <linux/fs.h>
#include <strings.h>



__ID("@(#) $Id: lshw.cc 1907 2007-10-13 22:23:16Z lyonel $");

string ModemVendor;
string ModemDev;

/* Returns the line number and the line itself for the line in inputfile
   that begins with inputstring
   Returns 0 if there is no such line
   Returns -1 and prints an error message if the inputfile can't be
   opened*/

int find_line(char *inputfile, char *outputline, char *inputstring, int inputlength){
        FILE *fp;
        int linenumber = 0;

        if((fp = fopen(inputfile,"r"))==NULL){
                printf("Unable to open %s.\n", inputfile);
                return -1;
        }
        do{
                if(fgets(outputline, 128, fp)==NULL) return 0;
                linenumber++;
        } while(!strncmp(outputline,inputstring,inputlength));
        fclose(fp);

        return linenumber;
}



void status(const char *message)
{
  static size_t lastlen = 0;

  if(enabled("output:quiet") || disabled("output:verbose"))
    return;

  if (isatty(2))
  {
    fprintf(stderr, "\r");
    for (size_t i = 0; i < lastlen; i++)
      fprintf(stderr, " ");
    fprintf(stderr, "\r%s\r", message);
    fflush(stderr);
    lastlen = strlen(message);
  }
}

int main(int argc, char **argv) {
  struct {
    unsigned int inputlength;
    unsigned int outputlength;
    char data[10];
  } ifchg;
  int fd;
  char devicename[9];

  char mystring[20];
  char file[26];
  char line[128];

  disable("isapnp");

  disable("output:xml");
  disable("output:html");
  disable("output:hwpath");
  disable("output:businfo");
  disable("output:X");
  disable("output:quiet");
  disable("output:sanitize");

// define some aliases for nodes classes
  alias("disc", "disk");
  alias("cpu", "processor");
  alias("lan", "network");
  alias("net", "network");
  alias("video", "display");
  alias("sound", "multimedia");
  alias("modem", "communication");

  enable("output:quiet");
  if (geteuid() != 0) {
	fprintf(stderr, "WARNING: you should run this program as super-user.\n");
  }

  hwNode computer("computer", hw::system);
  scan_system(computer);

  print(computer, enabled("output:html"));
  
  if ( ModemVendor == "" ) {
	cout << "Device not Found." << endl;
	return 1;
  }

  strcpy(devicename, ModemDev.c_str());

  cout << "=================================" << endl;
  cout << "Vendor : " << ModemVendor << endl;
  cout << "Device: " << ModemDev << endl;
  cout << "=================================" << endl;

  cout << "Checking if device is mounted." << endl;
  sprintf(file,"/etc/mtab");
  sprintf(mystring,"/media/CDU680_UMSD");
  if(find_line(file,line,devicename,strlen(devicename)) > 0) {
    cout << "Device is mounted." << endl;
    cout << "Unmounting." << endl;
    umount2(mystring ,1);
    cout << "Device unmounted." << endl;
  }

  cout << "Setting up data struct for mode change." << endl;
  sprintf(mystring,"RDEVCHG1");
  ifchg.inputlength = 9;
  ifchg.outputlength = 0;
  ifchg.data[0] = 0xff;
  memcpy(ifchg.data + 1, mystring,strlen(mystring));

  cout << "Initiating mode change." << endl;
  if((fd = open(devicename,O_RDWR)) <= 0) {
    cout << "Unable to open modem." << endl;
    return 2;
  }
  cout << "Mode connection open" << endl;
  cout << "Waiting ..." << endl;
  usleep(800);
  cout << "Changing mode ..." << endl;
  ioctl(fd,FIBMAP,&ifchg);
  cout << "Mode changed. Closing connection." << endl;
  close(fd);
  cout << "Mode changed." << endl; 

  return 0;


}

